package com.taomeng.servlet.admin;

import com.taomeng.constant.MessageConstant;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.RespBody;
import com.taomeng.entity.Student;
import com.taomeng.service.StudentService;
import com.taomeng.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {
    @Autowired
    private StudentService studentService;

    public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> map = req.getParameterMap();  //获取前端传来的json转化为map

        Student student = new Student();
        BeanUtils.populate(student, map);
        boolean flag = studentService.add(student);

        RespBody respBody = new RespBody();
        if (flag) {
            respBody.setCode(200);
            respBody.setMsg(MessageConstant.ADD_SUCCESSFUL);
        } else {
            respBody.setCode(500);
            respBody.setMsg(MessageConstant.ADD_FAILS);
        }
        RespBody.JsonObject(resp, respBody);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int page = Integer.parseInt(req.getParameter("page"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        String key = req.getParameter("key");

        PageBean<Student> pageBean = studentService.findByPage(page, limit, key);

        RespBody respBody = new RespBody();
        if (pageBean != null) {
            respBody.setCode(0);
            respBody.setMsg(MessageConstant.QUERY_SUCCESSFUL);
            respBody.setCount(pageBean.getTotal());
            respBody.setData(pageBean.getList());
        } else {
            respBody.setCode(500);
            respBody.setMsg(MessageConstant.QUERY_FAILS);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();

        Student student = new Student();
        BeanUtils.populate(student, map);

        boolean flag = studentService.updateStudent(student);

        RespBody respBody = new RespBody();
        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("更新成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("更新成功");
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");

        boolean flag = studentService.delStudentById(Integer.parseInt(id));


        RespBody respBody = new RespBody();

        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("删除成功" + id);
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败" + id);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void delBatchAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ids = req.getParameter("ids");
        //删除成功？
        boolean flag = studentService.delBatchAll(ids);


        RespBody respBody = new RespBody();
        if (flag) {
            respBody.setCode(200);
            respBody.setMsg("删除成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败");
        }

        RespBody.JsonObject(resp, respBody);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
